﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using CommonUtility;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using BLL;

namespace BLL
{
    public class bllMessage
    {

        /// <summary>
        /// 根据条件获取数据
        /// </summary>
        /// <param name="_read"></param>
        /// <param name="_operator"></param>
        /// <returns></returns>
        public static string GetList(string _read, UserInfo _operator)
        {
            string result = "";
            string where = "uid=" + _operator.RID;
            try
            {
                if (_read.Equals("0"))
                {
                    where += " and isnull(rDate,'')=''";
                }
                where += " order by cDate desc";
                IList<MessageInfo> list = new List<MessageInfo>();
                list = Message.GetList(where);
                string json = new JavaScriptSerializer().Serialize(list);
                result = @"{""Rows"":" + json + @",""Total"":""" + list.Count + @"""}";
            }
            catch (Exception ex)
            {
                result = "ERR";
                Common.Application_Error_Server("BLL/bllMessage.cs/GetList(" + _read + ") " + _operator.Name, ex);
            }
            return result;

        }


        /// <summary>
        /// 设置是否有效
        /// </summary>
        /// <param name="_funID"></param>
        /// <param name="_rids"></param>
        /// <param name="_oUser"></param>
        /// <returns></returns>
        public static string Process(string _rids, UserInfo _operator)
        {
            string result = string.Empty;
            System.Data.SqlClient.SqlTransaction myTran = SQLCommon.GetSqlTransaction();
            System.Data.SqlClient.SqlConnection myConn = myTran.Connection;
            try
            {
                IList<MessageInfo> list = Message.GetList("isnull(rDate,'') = '' and RID in (select * from dbo.f_splitSTR('" + _rids + "',','))");
                if (list.Count > 0)
                {
                    foreach (MessageInfo item in list)
                    {
                        item.RDate = DateTime.Now.ToString();
                        Message.Edit(item, myTran);
                    }
                    //添加操作记录
                    bllLog.AddLog("Message", "Read", _rids, _operator.UserID, myTran);

                    myTran.Commit();
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
                myTran.Rollback();
                Common.Application_Error_Server("BLL/bllMessage.cs/Process(" + _rids + ") " + _operator.Name, ex);
            }
            finally
            {
                myConn.Close();
                myTran.Dispose();
                myConn.Dispose();
            }

            return result;
        }

        /// <summary>
        /// 根据节点设置为已读
        /// </summary>
        /// <param name="_flowNode"></param>
        /// <param name="_operator"></param>
        /// <returns></returns>
        public static void SetRead(string _funID, UserInfo _operator)
        {
            System.Data.SqlClient.SqlTransaction myTran = SQLCommon.GetSqlTransaction();
            System.Data.SqlClient.SqlConnection myConn = myTran.Connection;
            try
            {
                IList<MessageInfo> list = Message.GetList("isnull(rDate,'')='' and funID ='" + _funID + "'");
                if (list.Count > 0)
                {
                    foreach (MessageInfo item in list)
                    {
                        item.RDate = DateTime.Now.ToString();
                        Message.Edit(item, myTran);
                    }
                    //添加操作记录
                    bllLog.AddLog("Message", "Read", "funID:" + _funID, _operator.UserID, myTran);

                    myTran.Commit();
                }
            }
            catch (Exception ex)
            {
                myTran.Rollback();
                Common.Application_Error_Server("BLL/bllMessage.cs/SetRead(funID:" + _funID + ") " + _operator.Name, ex);
            }
            finally
            {
                myConn.Close();
                myTran.Dispose();
                myConn.Dispose();
            }

        }

        /// <summary>
        /// 根据当前帐号获取记录条数
        /// </summary>
        /// <param name="_operator"></param>
        /// <returns></returns>
        public static string GetMessageCount(UserInfo _operator)
        {
            IList<MessageInfo> list = Message.GetList("isnull(rDate,'')='' and uid=" + _operator.RID);
            if (list.Count == 0)
            {
                return "";
            }
            else
            {
                return list.Count.ToString();
            }

        }


        /// <summary>
        /// 新增站内信息
        /// </summary>
        /// <param name="_uid">所属人RID</param>
        /// <param name="_uid">功能ID</param>
        /// <param name="_title">消息标题</param>
        /// <param name="_message">消息内容</param>
        /// <param name="_myTran"></param>
        public static void AddMessage(int _uid, string _funID, string _title, string _message, SqlTransaction _myTran)
        {
            MessageInfo item = new MessageInfo();
            item.Uid = _uid;
            item.FunID = _funID;
            item.Title = _title;
            item.Message = _message;
            item.CDate = DateTime.Now.ToString();
            Message.Add(item, _myTran);
        }

    }
}
